/**
 * 基础方法
 */
class Base {
    constructor() { };
    /**
     * 是function(){}
     */
    isFun(_v: Function) {
        if (_v instanceof Function) return true;
        return false;
    }
    /**
     * 类型为对象
     */
    isObj(_v: any) {
        if (_v instanceof Object) return true;
        return false;
    }
    /**
     * 类型为数组
     */
    isArr(_v: any) {
        if (_v instanceof Array) return true;
        return false;
    }
    /**
     * 类型为布尔
     */
    isBoo(_v: any) {
        if (typeof _v === "boolean") return true;
        return false;
    }
    /**
     * 类型为数字
     */
    isNum(_v: any) {
        if (typeof _v === "number") return true;
        return false;
    }
    /**
     * 类型为String
     */
    isStr(_v: any) {
        if (typeof _v === "string") return true;
        return false;
    }

    /**
     * 为空 ""、null、nudefined
     */
    isEmpty(_v: any) {
        if (_v === "" || _v === null || _v === undefined) return true;
        return false;
    }
    /**
     * 空数组 []
     */
    isEmptyArr(_v: any) {
        if (this.isEmpty(_v) || this.isArr(_v) && JSON.stringify(_v) === "[]") return true;
        return false;
    }
    /**
     * 空对象 {}
     */
    isEmptyObj(_v: any) {
        if (this.isEmpty(_v) || this.isObj(_v) && JSON.stringify(_v) === "{}") return true;
        return false;
    }
    /**
     * OBJECT,ARRAY 深克龙
     */
    clone(_v: any[] | object) {
        if (this.isEmptyArr(_v)) {
            return [];
        }
        if (this.isEmptyObj(_v)) {
            return {};
        }
        return JSON.parse(JSON.stringify(_v))
    }
    /**
     * Array to Tree
     */
    toTree(data: any[], key: string | number, parent: string | number) {
        if (!this.isArr(data)) {
            return;
        }
        if (this.isEmpty(parent) || !this.isStr(parent) || this.isEmpty(key) || !this.isStr(key)) {
            return;
        }
        let tree: any[] = [];
        let list: any[] = this.clone(data);
        let obj: any = {};
        list.map(item => {
            obj[item[key]] = item;
        });
        list.map(item => {
            if (obj[item[parent]]) {
                if (this.isEmpty(obj[item[parent]]["children"])) {
                    obj[item[parent]]["children"] = [];
                }
                obj[item[parent]]["children"].push(item);
            } else {
                tree.push(item)
            }
        });
        return tree;
    }
    /**
     * 遍历所有上级
     * @param {valueKey} 取值
     */
    toAllParent(data: any[], id: string | number, key: string | number, parent: string | number, valueKey: string | number) {
        let valuekey: string | number = valueKey || key;
        if (!this.isArr(data)) {
            return;
        }
        if (this.isEmpty(parent) || this.isEmpty(key)) {
            return;
        }
        let parents: any[] = [];
        let list = data;
        let obj: any = {};
        list.map(item => {
            obj[item[key]] = item;
        });
        // 递归
        let recursion = (Pid: string | number) => {
            if (this.isObj(obj[Pid])) {
                parents.push(obj[Pid][valuekey]);
                recursion(obj[Pid][parent]);
            }
        }
        recursion(id);
        return parents.reverse();
    }

}

export default Base;